package Service;


import dao.MusicMapper;
import pojo.Music;
import Service.MusicService;
import utils.MybatsUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;


//音乐服务层的具体实现

@Service("musicService")
public class MusicServiceImpl implements MusicService {
    //1.通过工具类生成SqlSession对象

    //2.实例化Mapper接口对象
    MusicMapper musicMapper=MybatsUtils.getMapper(MusicMapper.class);

    @Override
    public Music findMusicById(int id) {
        Music m = musicMapper.findMusicById(id);
        System.out.println("findMusicById  "+ m.toString());
        //关闭方法Session
        //sqlSession.close();
        return m;

    }

    @Override
    public List<Music> findAllMusics() {
        //方法Session执行Mapper接口中定义的方法，并返回映射结果
        List<Music> musicList = musicMapper.findAll();
//        System.out.println("findAllMusics"+musicList.toString());

        //关闭方法Session
        //sqlSession.close();
        return musicList;

    }

    @Override
    public int addMusic(Music m) {
        //方法Session执行Mapper接口中定义的方法，并返回映射结果
        int rows = musicMapper.addMusic(m);
        //提交事务

        return rows;

    }

    @Override
    public int updateMusic(Music m) {
        //执行方法Session的更新方法，返回的是方法语句影响的行数
        int rows = musicMapper.updateMusic(m);
        //提交事务

        return rows;

    }

    @Override
    public int deleteMusic(int id) {
        int rows = musicMapper.deleteMusic(id);
        //提交事务

        return  rows;
    }
}
